﻿#include "Coin.h"
//#include "Global.h"
#include "Resource.h"

Coin::Coin(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID) : Object(x, y)
{
	this->SetID(ID);
	_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\item_32.png"), 0, 0);


	_yOld = _y;
	_vy = 0;
	_accelY = 0;



	if (this->GetID() == 41)		//coin hien: 41, coin an: 46, 5coin an: 47
	{
		_sprite->SetFrame(6,6); //frame dong tien hien
		_numberCoin = 1;
		isHidden = false;
	}
	else if (this->GetID() == 46)
	{
		_sprite->SetFrame(7,7); //frame an
		_numberCoin = 1;
		isHidden = true;
	}
	else if (this->GetID() == 47)
	{
		_sprite->SetFrame(7,7); //frame an
		_numberCoin = 5;
		isHidden = true;
	}


	object_State = IS_IDLE;
}

Coin::~Coin()
{
	/*if (_sprite != NULL)
	{
	delete _sprite;
	}*/
}



void Coin::Render()
{
	if (object_State != IS_REMOVE)
	{
		_sprite->Render(_x, _y);
	}

}

OBJECT_TYPE Coin::GetTypeObject()
{
	return OBJECT_ITEM;
}

void Coin::ResetRect()
{
	_rectBond.left = _x;
	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
	_rectBond.top = _y;
	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;
}

void Coin::Update(list<Object*>* staObjs, list<Object*>* dynObjs, DxInput* input, float gameTime)
{
	//list<Object*>::iterator i;
	////Object* container = NULL;
	//Object* obj;
	//for(i=staObjs->begin();i!=staObjs->end();i++)
	//{
	//	obj=*i;
	//	if(obj->GetID()==21||obj->GetID()==25||obj->GetID()==26)
	//		if(Collision::RectVsRect(this->GetBond(),obj->GetBond()))
	//		{
	//			container=obj;
	//			//state=IS_IDLE;
	//			//if(obj->ID==24)			//Nếu tiền nằm trong gạch
	//			//{
	//			//	numCoin=5;
	//			//	obj->SetVar("isContainCoin",true);
	//			//}
	//		}
	//}

	switch(object_State)
	{
	case IS_IDLE:
		if (_numberCoin == 0)
		{
			/*if (this->GetID() == 47)
			{
			ChangeState(IS_5COIN);
			}
			else*/
			ChangeState(IS_REMOVE);
		}
		break;
	case IS_ACTIVING:
		if (_numberCoin != 0)
		{
			_vy += _accelY * gameTime;
			_y += _vy * gameTime + 1/2.0 * _accelY * gameTime *gameTime;
			if (_y > _yOld - 64 && _vy > 0)
			{

				_numberCoin--;

				if (_numberCoin == 0)
				{
					if (this->GetID() == 47)
					{
						container->ChangeState(BS_NONE);
					}

				}

				ChangeState(IS_IDLE);
			}
		}

		break;
	}
	_sprite->Update(gameTime);

	ResetRect();
}

void Coin::ChangeState(int state)
{
	object_State = state;
	switch(object_State)
	{
	case IS_IDLE:
		_y = _yOld;
		_vy = 0;
		_accelY = 0;
		_sprite->SetFrame(7,7);
		break;
	case  IS_ACTIVING:

		_vy = -1.8;
		_accelY = GRAVITY;
		_sprite->SetFrame(6,6);
		break;
	 
	case IS_REMOVE:
		break;
	}
}